﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Donors.aspx.cs" Inherits="DonationTracker.Donors" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css" >
        input.inline_btns 
        {
            display: inline-block;
        }
    </style>
    <script type="text/javascript" >
        function text_change() {
            // get the elements that we need
            var first = document.getElementById('MainContent_GridView1_txtNewFirstName');
            var last = document.getElementById('MainContent_GridView1_txtNewLastName');

            // get the value/length of each element
            var val1 = first.value;
            var l1 = val1.length;
            var val2 = last.value;
            var l2 = val2.length;
            // if the value is empty string, set length = 0
            if (val1 == "") { l1 = 0; }
            if (val2 == "") { l2 = 0; }

            // get btnSave
            var btn = document.getElementById('MainContent_GridView1_btnSave');
            // if the length of first > 0 and the length of last > 0, enable btnSave
            if (l1 > 0 && l2 > 0) {
                btn.disabled = false;
            } else { // if first or last or both are empty strings, disable btnSave
                btn.disabled = true;
            }
        }

        window.onload = function () {
            var d = document.getElementById('gridContainer');
            var g = document.getElementById('MainContent_GridView1');
            d.style.width = g.offsetWidth + "px";
            g.align = "center";
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Donors Page</h2>
    <br /><br />
    <asp:SqlDataSource ID="getType" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT DISTINCT [type] FROM [donor]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="donationTracker_dataSource" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        DeleteCommand="DELETE FROM [donor] WHERE [donorID] = @original_donorID AND [firstName] = @original_firstName AND [lastName] = @original_lastName AND [contactNumber] = @original_contactNumber AND (([email] = @original_email) OR ([email] IS NULL AND @original_email IS NULL)) AND (([organization] = @original_organization) OR ([organization] IS NULL AND @original_organization IS NULL)) AND (([type] = @original_type) OR ([type] IS NULL AND @original_type IS NULL))" 
        InsertCommand="INSERT INTO [donor] ([firstName], [lastName], [contactNumber], [email], [organization], [type]) VALUES (@firstName, @lastName, @contactNumber, @email, @organization, @type)" 
        OldValuesParameterFormatString="original_{0}" 
        SelectCommand="SELECT * FROM [donor]" 
        UpdateCommand="UPDATE [donor] SET [firstName] = @firstName, [lastName] = @lastName, [contactNumber] = @contactNumber, [email] = @email, [organization] = @organization, [type] = @type WHERE [donorID] = @original_donorID">
        <DeleteParameters>
            <asp:Parameter Name="original_donorID" Type="Int32" />
            <asp:Parameter Name="original_firstName" Type="String" />
            <asp:Parameter Name="original_lastName" Type="String" />
            <asp:Parameter Name="original_contactNumber" Type="Int64" />
            <asp:Parameter Name="original_email" Type="String" />
            <asp:Parameter Name="original_organization" Type="String" />
            <asp:Parameter Name="original_type" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="firstName" Type="String" />
            <asp:Parameter Name="lastName" Type="String" />
            <asp:Parameter Name="contactNumber" Type="Int64" />
            <asp:Parameter Name="email" Type="String" />
            <asp:Parameter Name="organization" Type="String" />
            <asp:Parameter Name="type" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="firstName" Type="String" />
            <asp:Parameter Name="lastName" Type="String" />
            <asp:Parameter Name="contactNumber" Type="Int64" />
            <asp:Parameter Name="email" Type="String" />
            <asp:Parameter Name="organization" Type="String" />
            <asp:Parameter Name="type" Type="String" />
            <asp:Parameter Name="original_donorID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div id="gridContainer" style="width: 800px; max-width: 960px; margin: 0px auto;" >
        <asp:GridView ID="GridView1" runat="server" 
            
            style="max-height: 400px; width: 800px; max-width: 960px; margin: 0px auto;" AllowPaging="True" 
            AutoGenerateColumns="False" DataKeyNames="donorID"  
            DataSourceID="donationTracker_dataSource" ShowHeaderWhenEmpty="True" 
            onrowcommand="GridView1_RowCommand" onrowupdating="GridView1_RowUpdating" >
            <Columns>
                <asp:TemplateField HeaderText="donorID" InsertVisible="False" 
                    SortExpression="donorID">
                    <FooterTemplate>
                        <asp:Button ID="btnSave" runat="server" Text="Save" CommandName="Insert" 
                            Enabled="False" />
                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" 
                            onclick="btnCancel_Click" CausesValidation="False" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("donorID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="firstName" SortExpression="firstName">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditFirstName" runat="server" Text='<%# Bind("firstName") %>' MaxLength="30" Width="100px" ></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewFirstName" runat="server" MaxLength="30" Width="100px" onkeyup="text_change()"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvNewFirstName" runat="server" 
                            ControlToValidate="txtNewFirstName" ErrorMessage="First Name is required." 
                            ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("firstName") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle VerticalAlign="Middle" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="lastName" SortExpression="lastName">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditLastName" runat="server" Text='<%# Bind("lastName") %>' MaxLength="40" Width="100px" ></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewLastName" runat="server" MaxLength="40" Width="100px" onkeyup="text_change()"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvNewLastName" runat="server" 
                            ControlToValidate="txtNewLastName" ErrorMessage="Last Name is required." 
                            ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("lastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="contactNumber" SortExpression="contactNumber">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditContactNumber" runat="server" Text='<%# Bind("contactNumber") %>'
                            MaxLength="10" Width="75px" ></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewContactNumber" runat="server" MaxLength="10" 
                            Width="75px"></asp:TextBox>
                        <asp:RegularExpressionValidator ID="regexNewContactNumber" runat="server" 
                            ControlToValidate="txtNewContactNumber" ErrorMessage="Please enter a 10-digit phone number." 
                            ForeColor="Red" ValidationExpression="\d{10}" Display="Dynamic"></asp:RegularExpressionValidator>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# String.Format("{0:(###) ###-####}", Convert.ToInt64(DataBinder.Eval(Container.DataItem, "contactNumber"))) %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="email" SortExpression="email">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditEmail" runat="server" Text='<%# Bind("email") %>'
                            MaxLength="50" ></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewEmail" runat="server" MaxLength="50"></asp:TextBox>
                        <asp:RegularExpressionValidator ID="regexNewEmail" runat="server" 
                            ControlToValidate="txtNewEmail" ErrorMessage="Example: matt@webmail.com" 
                            ForeColor="Red" 
                            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                            Display="Dynamic"></asp:RegularExpressionValidator>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="organization" SortExpression="organization">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditOrganization" runat="server" Text='<%# Bind("organization") %>' 
                            MaxLength="50" ></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewOrganization" runat="server" MaxLength="50"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label6" runat="server" Text='<%# Bind("organization") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="type" SortExpression="type">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlEditDonorType" runat="server" DataSourceID="getType" 
                            DataTextField="type" DataValueField="type">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlDonorType" runat="server" DataSourceID="getType" 
                            DataTextField="type" DataValueField="type">
                        </asp:DropDownList>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label7" runat="server" Text='<%# Bind("type") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnAddDonor" runat="server" Text="Add Donor" 
            onclick="btnAddDonor_Click" />
    </div>
</asp:Content>